package thread;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/26 17:12
 * 测试线程停止
 * 1，建议让他自己停止，最好不要死循环
 * 2，不适用jdk不推荐使用的方法，stop ，destory
 * 3，设置一个标识，通过改变标识让线程停止
 */
public class ThreadStop implements Runnable{
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag){
            System.out.println("run..." + i++);
        }
        System.out.println("stop");
    }

    private void stop(){
        flag = false;
    }

    public static void main(String[] args) {
        ThreadStop threadStop = new ThreadStop();
        new Thread(threadStop).start();
        for (int i = 0; i < 100000000; i++) {
            if (i==90000000){
                threadStop.stop();
            }
        }
    }
}
